import { reactive,} from "vue";
import { Api } from "../../api/api.js";
import { ElMessage, ElNotification } from 'element-plus';
import router from "../../router/index.js";
import { GJ_nowTime, GJ_randomStr } from "../../../static/js/tool.js";

function loginJS() {
    //登录数据
    let login = reactive({
        user: "",
        passwd: "",
    })
    //不允许其他人注册
    // let reg = ref("");
    //注册数据
    let sign = reactive({
        user: "",
        passwd: "",
        email: "",
        tel: "",
        createTime: "",
        lastTime: ""
    })
    //控制显示隐藏数据
    let show_hide = reactive({
        //注册框firewall-cmd --list-ports
        signBox: false,
    })
    //方法函数
    let methodFun = {
        //登录
        loginFun: () => {
            if (login.user != "" && login.password != "") {
                // login.newDate = "2011-11-04 09:11:56";
                login.newDate = GJ_nowTime("date-time", "-");
                Api.login(login).then((res) => {
                    // console.log(res.data.data);
                    // 判断用户是否存在
                    if (res.data.data.length != 0) {
                        //判断用户是否被禁用
                        if (res.data.data[0].disable) {
                            window.sessionStorage.setItem("userinfo", JSON.stringify(res.data.data[0]));

                            ElNotification({
                                type: "success",
                                title: "登录成功",
                                message: "欢迎回家",
                                duration: 1000,
                            });
                            Api.userChage({
                                user: login.user,
                                change: "lastTime",
                                // newDate: "2021-08-09 12:12:12",
                                newDate: GJ_nowTime("date-time", "-"),
                            }).then(() => {
                                Api.userFind({ user: login.user }).then((resi) => {
                                    console.log(resi.data, 123);
                                    window.sessionStorage.setItem("userinfo", JSON.stringify(resi.data.data[0]));
                                });
                                Api.log({
                                    user: login.user,
                                    createTime: GJ_nowTime("date-time", "-"),
                                    type: 0,
                                }).then((res) => {
                                    console.log(res.data, "log");
                                });
                            });
                            window.localStorage.setItem("user", login.user);
                            window.localStorage.setItem("passwd", login.passwd);
                            setTimeout(() => {
                                router.push("/home");
                            }, 1200);
                        } else {
                            ElNotification({
                                title: "你的账号已禁用",
                                type: "warning",
                                message: "详情请咨询管理员：997884551"
                            })
                        }
                    } else {
                        ElMessage.error("用户名或密码错误！");
                    }
                })
            } else {
                ElMessage.warning("用户名或密码不能为空！");
            }
        },
        //弹出(隐藏)注册框
        signBoxFun: () => {
            sign.user = "";
            sign.email = "";
            sign.email = "";
            sign.tel = "";
            sign.createTime = "";
            sign.lastTime = "";
            show_hide.signBox = !show_hide.signBox;
        },
        //确定注册
        confirmSign: () => {
            // if (reg.value == "wuyu") {//就算你会看代码填对了邀请码
                // let type = Object.prototype.toString;
                // if (type.call(reg.value) != "12345") {//我也不给你注册
                //     ElNotification({
                //         title: "邀请码只能是数字哦",
                //         type: "warning",
                //         message: "偷偷告诉你、管理员已经关闭了对外注册哦！",
                //         duration: 1000,
                //     });
                //     return;
                // }
                let telReg = new RegExp("^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$");
                let emailReg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
                let resultarr = [];
                resultarr.push(((sign.user) + "").length >= 6);
                resultarr.push(((sign.passwd) + "").length >= 6);
                resultarr.push(telReg.test(sign.tel));
                resultarr.push(emailReg.test(sign.email));
                console.log(resultarr, sign);
                let result = resultarr.every((item) => {
                    return item;
                });
                if (!result) {
                    ElNotification({
                        title: "注册失败",
                        type: "error",
                        message: "信息填写不完整或不合法",
                        duration: 1000,
                    });
                } else {
                    sign.createTime = GJ_nowTime("date-time", "-");
                    sign.lastTime = GJ_nowTime("date-time", "-");
                    sign.userName = `神秘用户_${GJ_randomStr(8)}`;
                    Api.signAdd(sign).then((res) => {
                        if (res.data) {
                            ElNotification({
                                title: "注册成功",
                                type: "success",
                                message: "即将跳转登录界面",
                                duration: 1000,
                            });
                            setTimeout(() => {
                                methodFun.signBoxFun();
                            }, 800);
                        } else {
                            ElNotification({
                                title: "注册失败",
                                type: "error",
                                message: "用戶名已存在",
                                duration: 1000,
                            });
                        }
                    })
                        .catch((err) => {
                            console.log(err);
                        });
                }
            // } else {
            //     ElNotification({
            //         title: "邀请码不正确",
            //         type: "error",
            //         message: "只对拥有邀请码的人开放注册哦、快联系管理员吧！QQ：997884551",
            //         duration: 1000,
            //     });
            // }
        }
    }
    return {
        login,
        sign,
        show_hide,
        methodFun
    }
}


export default loginJS;